package com.uqbar.game.stateSpacecraft;

import java.util.List;
import java.util.ArrayList;
import com.uqbar.game.Bullet;
import com.uqbar.game.Spacecraft;
import com.uqbar.game.asteroid.tipe.Asteroid;
import com.uqbar.vainilla.DeltaState;
import com.uqbar.vainilla.appearances.Sprite;
import com.uqbar.vainilla.colissions.CollisionDetector;
import com.uqbar.vainilla.events.constants.Key;
import com.uqbar.vainilla.sound.Sound;
import com.uqbar.vainilla.sound.SoundBuilder;

public class StatePlay extends StateSpacecraft{

	private Sprite spriteSpacecraftPlay;
	private Sound soundExploitAsteroid = new SoundBuilder().buildSound("/sound/explosion/ex2.WAV");
	private List<Bullet> bullets = new ArrayList<Bullet>();
	
	public StatePlay(Spacecraft spacecraft) {
		super(spacecraft);
		this.spriteSpacecraftPlay = Sprite.fromImage("/spacecraft/nave.png");
		this.spacecraft.setAppearance(spriteSpacecraftPlay);
	}

	public void moveSpacecraft(DeltaState deltaState) {
		this.move(deltaState);
		this.rotateSpacraft(deltaState);
		this.spacecraft.align(this.spacecraft.getDiameter());
		this.shot(deltaState);
		this.exploitWithAsteroid();
		System.out.println(this.spacecraft.getAngle());

	}
	
	private void shot(DeltaState deltaState) {
		//si dispara
		if(deltaState.isKeyPressed(Key.SPACE)){
			Bullet bullet =new Bullet(this.spacecraft.getX(), this.spacecraft.getY(), this.movX, this.movY, this.spacecraft.getAsteroids(), 
					this.spacecraft, this.spacecraft.getR());
			bullets.add(bullet);
			this.spacecraft.getGame().getCurrentScene().addComponent(bullet);
		}
	}
	private void exploitWithAsteroid() {
		CollisionDetector collision = CollisionDetector.INSTANCE;
		
		
		for (Asteroid asteroid : this.spacecraft.getAsteroids()) {
			if(collision.collidesCircleAgainstCircle(this.spacecraft.getX(), this.spacecraft.getY(), this.spacecraft.getDiameter(), 
					asteroid.getX(), asteroid.getY(), asteroid.getDiameter())){
				this.spacecraft.setState(new StateDestroy(spacecraft));
				this.soundExploitAsteroid.play();
				this.spacecraft.lostOneLife();
			}
		}
	}

	@Override
	public Sprite getImage() {
		return this.spriteSpacecraftPlay;
	}

	public void setSoundExploitAsteroid(Sound soundExploitAsteroid) {
		this.soundExploitAsteroid = soundExploitAsteroid;
	}

	public Sound getSoundExploitAsteroid() {
		return soundExploitAsteroid;
	}
	
	public List<Bullet> getBullets() {
		return bullets;
	}

	public void setBullets(List<Bullet> bullets) {
		this.bullets = bullets;
	}
	
}
